<script lang="ts" setup>
import { ref, onMounted } from "vue"
import { createBuildTypeApi, updateBuildTypeApi, getBuildTypeDetailApi } from "@/api/buildType"
import { type CreateOrUpdateBuildTypeRequestData } from "@/api/buildType/types"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import { useRouter } from "vue-router"
import ProductsSelect from "./components/ProductsSelect.vue"
defineOptions({
  // 命名当前组件
  name: "BuildTypeOperate"
})
const router = useRouter()
const id: string = router.currentRoute.value.params.id as string
const loading = ref<boolean>(false)

const gotoList = () => {
  router.push("/buildType")
}
//#region 增
const DEFAULT_FORM_DATA: CreateOrUpdateBuildTypeRequestData = {
  id,
  name: "",
  name_en: "",
  product_id: ""
}
const formRef = ref<FormInstance | null>(null)
const formData = ref<CreateOrUpdateBuildTypeRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<CreateOrUpdateBuildTypeRequestData> = {
  name: [{ required: true, trigger: "blur", message: "请输入类型名称" }],
  product_id: [{ required: true, trigger: "blur", message: "请选择代表产品" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const api = !formData.value.id ? createBuildTypeApi : updateBuildTypeApi
    api(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
        resetForm()
        gotoList()
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = JSON.parse(JSON.stringify(DEFAULT_FORM_DATA))
}

//#region 查
const getBuildTypeDetail = () => {
  if (!id) {
    return
  }
  getBuildTypeDetailApi(id)
    .then(({ data }) => {
      formData.value = data
    })
    .catch(() => {
      formData.value = DEFAULT_FORM_DATA
    })
}
onMounted(() => {
  getBuildTypeDetail()
})
</script>

<template>
  <div class="app-container">
    <!-- 新增/修改 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>{{ id === undefined ? "新增建筑类型" : "修改建筑类型" }}</h3>
        </div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="name" label="类型名称">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="name_en" label="类型名称(英文)">
          <el-input v-model="formData.name_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="product_id" label="代表产品">
          <ProductsSelect v-model="formData.product_id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="gotoList">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确定</el-button>
      </template>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
@/api/buildType@/api/buildType/types
